package enron.Generator;

import java.util.ArrayList;

import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

import enron.VendorAgent;

public class SolarHeaterTurbine extends Generator {
	
	public static Long defaultStartUpTime = 10L;
	private static JPanel generatorOptionsGUI;
	private static JFormattedTextField txtMaximumCapacity;
	private static JFormattedTextField txtMinimumCapacity;

	public SolarHeaterTurbine(VendorAgent agent, String newName, Long max, Long min) {
		super(agent, newName, max, min, defaultStartUpTime);
	}
	
	public SolarHeaterTurbine(VendorAgent agent, String newName, ArrayList<Object> args) {
		super(agent, newName, (Long) args.get(0), (Long) args.get(1), defaultStartUpTime);
	}
	
	/**
	 * @wbp.parser.constructor
	 */
	public SolarHeaterTurbine() {
		super(null,0L,0L,0L);
	}

	@Override
	public Double getTurnDownCost(Long reductionAmount) {
		Double turnDownCost = 0.0;
	
		//Calculate turnDownCost
		if((getCurrentSupply() - reductionAmount) < getMinimumSupply()) {
			turnDownCost = getTurnOffCost();
		} else {
			turnDownCost = reductionAmount.doubleValue()*(19.0/(getMaximumSupply().doubleValue()-getMinimumSupply().doubleValue()))+1;
		}
		
		return turnDownCost;
	}
	
	/**
	 * @wbp.parser.entryPoint
	 */
	public static JPanel getGeneratorOptionsGUI() {
		if(generatorOptionsGUI == null) {
			generatorOptionsGUI = new JPanel();
			generatorOptionsGUI.setName("SolarHeaterTurbine");
			generatorOptionsGUI.setLayout(new FormLayout(new ColumnSpec[] {
					FormFactory.DEFAULT_COLSPEC,
					FormFactory.RELATED_GAP_COLSPEC,
					ColumnSpec.decode("default:grow"),},
				new RowSpec[] {
					FormFactory.DEFAULT_ROWSPEC,
					FormFactory.RELATED_GAP_ROWSPEC,
					FormFactory.DEFAULT_ROWSPEC,}));
			{
				JLabel lblMaximumCapacity = new JLabel("Maximum Capacity:");
				lblMaximumCapacity.setName("lblMaximumCapacity");
				generatorOptionsGUI.add(lblMaximumCapacity, "1, 1, right, default");
			}
			{
				txtMaximumCapacity = new JFormattedTextField();
				txtMaximumCapacity.setText("Maximum Capacity");
				txtMaximumCapacity.setName("txtMaximumCapacity");
				generatorOptionsGUI.add(txtMaximumCapacity, "3, 1, fill, default");
				txtMaximumCapacity.setColumns(10);
			}
			{
				JLabel lblMinimumAcceptableCapacity = new JLabel("Minimum Acceptable Capacity:");
				lblMinimumAcceptableCapacity.setName("lblMinimumAcceptableCapacity");
				generatorOptionsGUI.add(lblMinimumAcceptableCapacity, "1, 3, right, default");
			}
			{
				txtMinimumCapacity = new JFormattedTextField();
				txtMinimumCapacity.setText("Minimum Capacity");
				txtMinimumCapacity.setName("txtMinimumCapacity");
				generatorOptionsGUI.add(txtMinimumCapacity, "3, 3, fill, default");
				txtMinimumCapacity.setColumns(10);
			}
		}
		return generatorOptionsGUI;
	}
	
	static public Generator parseOptions(String name) {
		Generator returnVal = null;
		if(generatorOptionsGUI != null) {
			returnVal = new SolarHeaterTurbine(null, name, Long.parseLong(txtMaximumCapacity.getText())*1000L, Long.parseLong(txtMinimumCapacity.getText())*1000L);
		}
		return returnVal ;
	}
}
